package com.sanjishu.enterlab.electricity.sale.service;

import com.sanjishu.enterlab.electricity.sale.domain.City;
import com.sanjishu.enterlab.electricity.sale.mapper.CityMapper;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class CityService {

    @Autowired
    private CityMapper cityMapper;

    public List<City> getCityList() {
        return cityMapper.findCityList();
    }

    public City getById(Integer id) {
        return cityMapper.selectByPrimaryKey(id);
    }

    public City getParentById(Integer id) {
        return cityMapper.getParentById(id);
    }

    public City getAreaAndCity(Integer id) {
        return cityMapper.getAreaAndCity(id);
    }

    public List<City> getArea(Integer id) {
        return cityMapper.getArea(id);
    }

    public List<City> allCity() {
        return cityMapper.selectAll();
    }

    public HashMap<String, List<City>> returnCityMap() {
        HashMap<String, List<City>> cityMap = new HashMap<>();
        List<City> cityList = allCity();
        if (cityList != null && cityList.size() > 0) {
            for (City city : cityList) {
                List<City> tempList = cityMap.get(city.getParentId().toString());
                if (tempList == null || tempList.size() == 0) {
                    tempList = new ArrayList<>();
                }
                tempList.add(city);
                cityMap.put(city.getParentId().toString(), tempList);
            }
        }
        return cityMap;
    }
}
